package com.yunzhong.appointment.essentialinformation.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.yunzhong.appointment.essentialinformation.util.FileUtils;

@RestController
@RequestMapping("/api/v1/files/")
public class FileController {private static Logger logger = LoggerFactory.getLogger(FileController.class);

    @PostMapping("upload")
    public Map<String,Object> upload(@RequestParam(value = "file") MultipartFile[] files,
                      @RequestParam(name = "fileDir", required = false)String fileDir
    ) throws IOException {
      /*  {
            "success": true/false,
                "msg": "error message", # optional
            "file_path": "[real file path]"
        }*/

        List<String> list = new ArrayList<>();
        Map<String,Object> responseBean = new HashMap<String,Object>();
        for (MultipartFile file : files) {
            String path = FileUtils.saveFile(file);
            list.add(path);
        }
        responseBean.put("success",true);
        responseBean.put("msg","ok");
        responseBean.put("file_path",list);
        return responseBean;
    }

}
